
package com.wulian.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wulian.common.constants.CommonConstants;
import com.wulian.user.entity.SysMenu;
import com.wulian.user.entity.SysRoleMenu;
import com.wulian.user.mapper.SysMenuMapper;
import com.wulian.user.mapper.SysRoleMenuMapper;
import com.wulian.user.service.SysRoleMenuService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2017-10-29
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
   
    @Autowired
    private SysMenuMapper sysMenuMapper;
    
	@Override
    @CacheEvict(value = "menu_details", key = "#role + '_menu'")
    public Boolean insertRoleMenus(String role, Integer roleId, Integer[] menuIds) {
        SysRoleMenu condition = new SysRoleMenu();
        condition.setRoleId(roleId);
        this.delete(new EntityWrapper<>(condition));
        //菜单id数组转换为字符串
      /*  String menuIdStr = StringUtils.join(menuIds, ",");
        
        List<String> list = new ArrayList<String>();
        list.add("9999999");*/
        List<SysRoleMenu> roleMenuList = new ArrayList<>();
        for (Integer menuId : menuIds) {
        	/*SysMenu sysMenu = sysMenuMapper.selectById(menuId);
        	if(null != sysMenu && CommonConstants.DEL_FLAG_NO.equals(sysMenu.getDelFlag())){
        		Integer parentId = sysMenu.getParentId();
        		
        		if(-1 != parentId.intValue() && !menuIdStr.contains(String.valueOf(parentId)) 
        				&& !list.contains(String.valueOf(parentId))){
        			
        			list.add(String.valueOf(parentId));
        			
        			SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(parentId);
                    roleMenuList.add(roleMenu);
        		}
        	}*/
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuList.add(roleMenu);
        }
        return this.insertBatch(roleMenuList);
    }
}
